package com.edu.admin.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import com.edu.admin.filter.SysAuthenticationEntryPoint;
import com.edu.admin.filter.SysAuthenticationFilter;

/**
 * 
 * @author Wangjiong
 * 
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    private final SysAuthenticationEntryPoint authorizedEntryPoint;

    private final AccessDeniedHandler accessDeniedHandler;

    private final UserDetailsService adminDetailsService;

    private final SysAuthenticationFilter authenticationFilter;

    @Autowired
    public WebSecurityConfig(SysAuthenticationEntryPoint authorizedEntryPoint,
                             @Qualifier("RestAuthenticationAccessDeniedHandler") AccessDeniedHandler accessDeniedHandler,
                             @Qualifier("AdminDetailsService") UserDetailsService adminDetailsService,
                             SysAuthenticationFilter authenticationFilter) {
        this.authorizedEntryPoint = authorizedEntryPoint;
        this.accessDeniedHandler = accessDeniedHandler;
        this.adminDetailsService = adminDetailsService;
        this.authenticationFilter = authenticationFilter;
    }

    @Autowired
    public void configureAuthentication(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
        authenticationManagerBuilder
                .userDetailsService(this.adminDetailsService)
                .passwordEncoder(passwordEncoder());
    }

    /**
     * 装载BCrypt密码编码器
     * @return
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity
                .exceptionHandling().accessDeniedHandler(accessDeniedHandler).and()
                .csrf().disable()
                .exceptionHandling().authenticationEntryPoint(authorizedEntryPoint).and()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
                .authorizeRequests()
                .antMatchers("/login","/sys/**", "/district/list","/admin/updatepwd","/static/**","/druid/**").permitAll()
                .anyRequest().authenticated();

        httpSecurity.headers().cacheControl();
        httpSecurity.headers().frameOptions().sameOrigin();
        // 添加filter
        httpSecurity
                .addFilterBefore(authenticationFilter, UsernamePasswordAuthenticationFilter.class);
    }

    @Override
    public void configure(WebSecurity web) {
        web
                .ignoring()
                .antMatchers(
                        "index.html",
                        "/favicon.ico",
                        "/static/**",
                        "/**/*.css",
                        "/**/*.js",
                        "/**/*.png",
                        "/**/*.gif",
                        "/**/*.map",
                        "/**/*.woff",
                        "/**/*.svg",
                        "/apidoc/**",
                        "/html/**"
                );

    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
}